1158A - The Party and Sweets - CodeForces Solution


binary search constructive algorithms greedy implementation math sortings two pointers *1500

Please click on ads to support us..

Python Code:

def solve():
    n, m = map(int, input().split())
    b = list(map(int, input().split()))
    g = list(map(int, input().split()))

    if max(b) > min(g):
        print(-1)
        return

    if max(b) == min(g):
        ans = sum(b) * m - max(b) * m + sum(g)
        print(ans)
        return

    max_val = 0
    second_max = 0
    for i in b:
        if i > max_val:
            second_max = max_val
            max_val = i
        elif i > second_max:
            second_max = i

    ans = sum(b) * m - (max_val * (m - 1)) - second_max + sum(g)
    print(ans)
solve()

C++ Code:

#include <bits/stdc++.h>
#define io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define ll long long
using namespace std;

typedef pair<int, int> pii;

const ll MOD = 1e9L + 7;
const int INF = 1e9+5;
const int N = 1e5+10;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
char dir[] = {'U', 'L', 'D', 'R'};

template <typename T> void min_self(T &a, T b) { a = min(a, b); }
template <typename T> void max_self(T &a, T b) { a = max(a, b); }

bool cmp(pii &a, pii &b) {
    return a.S < b.S;
}

int main() {
    io;
    int n, m;
    cin >> n >> m;
    ll bsum = 0, gsum = 0;
    ll bmx = 0, gmn = LLONG_MAX, scd_bmx = 0;

    // Read and calculate the maximum and second maximum values for the first set
    for (int i = 0; i < n; i++) {
        ll d;
        cin >> d;
        if (d > bmx) {
            scd_bmx = bmx;
            bmx = d;
        } else if (d > scd_bmx) {
            scd_bmx = d;
        }
        bsum += d;
    }

    // Read and calculate the minimum value for the second set
    for (int j = 0; j < m; j++) {
        ll d;
        cin >> d;
        min_self(gmn, d);
        gsum += d;
    }

    ll cur = 0;

    if (bmx > gmn) {
        // If the maximum value in the first set is greater than the minimum in the second set, it's impossible.
        puts("-1");
        return 0;
    }

    if (bmx == gmn) {
        // If the maximum value in the first set equals the minimum in the second set,
        // calculate the result as described in the code.
        cur += m * bsum;
        cur += gsum;
        cur -= bmx * m;
        cout << cur << "\n";
    } else {
        // If the maximum value in the first set is less than the minimum in the second set,
        // calculate the result as described in the code.
        cur += m * bsum;
        cur += gsum;
        cur -= (m - 1) * bmx + scd_bmx;
        cout << cur << "\n";
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection
1665D - GCD Guess
29A - Spit Problem
1097B - Petr and a Combination Lock
92A - Chips
1665B - Array Cloning Technique
1665A - GCD vs LCM